// pages/video/index.js
import {getVideoData} from '../../services/video-api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mvList:[],
    currentPage:1,
    hasMore:true
  },
  onLoad: function (options) {
    const offset = (this.data.currentPage - 1) * 30
    this.getPageData(offset)
  },
  getPageData(offset){
    getVideoData(offset).then(res => {
      if(!this.data.hasMore) return 
      wx.showNavigationBarLoading()
      const hasMore = res.hasMore
      this.setData({hasMore})
      const mvList = this.data.mvList.concat(res.data)
      this.setData({mvList})
      wx.hideNavigationBarLoading()
    })
  },
  goToMvDetail(e){
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../video-detail/index?id=${id}`,
    })
  },
  onReachBottom: function () {
    if(!this.data.hasMore) return wx.showToast({
      title: '已经到底啦~',
    })
    const offset = this.data.currentPage * 30
    this.setData({currentPage:this.data.currentPage + 1})
    this.getPageData(offset)
  },
  onPullDownRefresh(){
    const currentPage = 1 
    const hasMore = true 
    this.setData({currentPage,hasMore,mvList:[]})
    this.getPageData(0)
  }
})